//
//  JGBoutiqueVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/19.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "JGBoutiqueVC.h"
#import "ListModel.h"
#import "JGOnlyListenTableViewCell.h"
#import "EditorTableViewCell.h"

@interface JGBoutiqueVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *backTableV;/**< 底层tableview */
@property (nonatomic, strong) NSMutableArray *modelArr;/**< model数组 */
@property (nonatomic, strong) UIView *topView;/**< 头视图 */
@property (nonatomic, strong) ListModel *myModel;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, strong) NSMutableArray *modelArrOther;
@property (nonatomic, assign) BOOL isUpStata;

@end

@implementation JGBoutiqueVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"听单";
    self.modelArr = [NSMutableArray array];
    self.modelArrOther = [NSMutableArray array];
    
    self.isUpStata = YES;
    
    [self createView];
//    [self addFooter];
    
    
}
#pragma mark - 创建view
- (void)createView{
    self.backTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HETGHT - 113) style:UITableViewStylePlain];
    [self.view addSubview:_backTableV];
    self.backTableV.dataSource = self;
    self.backTableV.delegate = self;
    self.backTableV.rowHeight = 90;
    
    // 注册cell
    [self.backTableV registerClass:[JGOnlyListenTableViewCell class] forCellReuseIdentifier:@"JGOnlyListenTableViewCell"];
    [self.backTableV registerClass:[EditorTableViewCell class] forCellReuseIdentifier:@"EditorTableViewCell"];
    
}
#pragma mark - 创建头视图
- (void)createTopView{
    
    // 自定义高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
    // 通过文字大小 获取文本的高度
    self.rect = [self.myModel.intro boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    // 创建头视图
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.rect.size.height + 100)];
    
    self.backTableV.tableHeaderView = _topView;
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    imageV.backgroundColor = [UIColor whiteColor];
    [self.topView addSubview:imageV];
    dispatch_async(dispatch_get_main_queue(), ^{
        
        imageV.image = [UIImage imageNamed:@"findsubject_cover@3x"];
    });
    [imageV.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(55, 10, SCREEN_WIDTH - 65, 50)];
    titleL.text = self.myModel.title;
    titleL.numberOfLines = 2;
    [self.topView addSubview:titleL];
    
    UILabel *otherL = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, 20)];
    otherL.text = @"-----◇  简介  ◇-----";
    otherL.font = [UIFont systemFontOfSize:13];
    otherL.textColor = [UIColor grayColor];
    [self.topView addSubview:otherL];
    otherL.textAlignment = NSTextAlignmentCenter;
    
    UILabel *digestL = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, SCREEN_WIDTH - 20, self.rect.size.height)];
    digestL.text = self.myModel.intro;
    digestL.numberOfLines = 50;
    [self.topView addSubview:digestL];
    digestL.font = [UIFont systemFontOfSize:12];
    digestL.textColor = [UIColor grayColor];
    
    UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(10, self.rect.size.height + 80, SCREEN_WIDTH - 20, 20)];
    NSString *str = [NSString stringWithFormat:@"小编 : %@", self.myModel.nickname];
    nameL.text = str;
    [self.topView addSubview:nameL];
    nameL.textAlignment = NSTextAlignmentRight;
    nameL.font = [UIFont systemFontOfSize:12];
    nameL.textColor = [UIColor grayColor];
    
    
}
#pragma mark - tableview的协议方法
// 返回cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArr.count;
}
// 返回cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListModel *model = [[ListModel alloc] init];
    model = self.modelArr[indexPath.row];
    
    if ([self.myModel.contentType isEqual:@2]) {
        JGOnlyListenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JGOnlyListenTableViewCell"];
        cell.model = model;
        return cell;
        
    } else{
        EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditorTableViewCell"];
        cell.model = model;
//        NSLog(@"%@", model.albumCoverUrl290);
        return cell;
        
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MusicPlayerViewController *MPVC = [[MusicPlayerViewController alloc] init];
    
    MPVC.indexNum = indexPath.row;
    
    MPVC.playMusicArr = self.modelArrOther;
    
    [self presentViewController:MPVC animated:YES completion:^{
        
    }];

    
}



#pragma mark - 数据解析
- (void)dataHander{
    
    [APPTools POSTWithURL:@"http://mobile.ximalaya.com/m/subject_detail" par:@{@"id":self.specialId,@"device":@"iPhone"} success:^(id responseObject) {
        
        NSArray *arr = [responseObject objectForKey:@"list"];
        
        if (_isUpStata == NO) {
            self.modelArr = [ListModel modelHandleWithArray:arr];
            
            self.modelArrOther = [ListenModel modelHandleWithArray:arr];
        }else{
            for (NSDictionary *dic in arr) {
                ListModel *model = [[ListModel alloc] init];;
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArr addObject:model];
            }
            for (NSDictionary *dic in arr) {
                ListenModel *model = [[ListenModel alloc] init];;
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArrOther addObject:model];
            }


        }
        
        self.myModel = [[ListModel alloc] init];
        [self.myModel setValuesForKeysWithDictionary:[responseObject objectForKey:@"info"]];
        
        [self createTopView];
        
        [self.backTableV reloadData];
        [self.backTableV.mj_footer endRefreshing];
        
    } filed:^(NSError *error) {
        
        
        
    }];
}

/**< 上拉加载 */
- (void)addFooter{
    self.backTableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 标记为上拉
        _isUpStata = NO;
        
        [self dataHander];
        
    }];
    
    [self.backTableV.mj_footer beginRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
